package com.novel.common.job;

import com.novel.service.UserService;
import com.novel.utils.DateLocationUtils;
import lombok.extern.slf4j.Slf4j;
import org.quartz.JobExecutionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Component;


/**
 * 每月月初给所有用户发送推荐票
 */
@Slf4j
@Component
@PropertySource("/manager.properties")
public class FreeSendRecommendJob extends QuartzJobBean {

    @Autowired
    private UserService userService;

    @Value("${manager.month.user.commend.count}")
    private String recommendCount;

    @Override
    protected void executeInternal(JobExecutionContext jobExecutionContext) {
        try {
            if (DateLocationUtils.checkIsFirstDayForMonth()) {
                userService.sendAllUserRecommend(Integer.parseInt(recommendCount));
                log.info("今天是本月第一天,推荐票发放完成!");
            }
            log.info("今天不是本月第一天!");
        } catch (Exception e) {
            log.error("数据库操作失败! 请及时检查!", e);
        }
    }


}